﻿
using System;
using System.Collections.Generic;
using Test30.Util;

namespace Test30.Engine
{
    public class EntityConditions
    {
        internal event EventHandler<EventArgs> ConditionChanged;
        public Condition Poison { get; }
        public Condition Haste { get; }
        public Condition Freeze { get; }
        public Condition Slow { get; }

        public EntityConditions(NotNull<Entity> entity)
        {
            Poison = new Condition(entity, theEntity => new PoisonDamageAction(theEntity),
                theEntity => new PoisonCompleteAction(theEntity));
            Add(Poison);
            Haste = new Condition(entity, theEntity => new HasteCompleteAction(theEntity));
            Add(Haste);
            Freeze = new Condition(entity, theEntity => new FreezeCompleteAction(theEntity));
            Add(Freeze);
            Slow = new Condition(entity, theEntity => new SlowCompleteAction(theEntity));
            Add(Slow);
        }
        public IEnumerable<Action> Update()
        {
            foreach (Condition condition in _conditions)
            {
                Action action = condition.Update();
                if (action != null) yield return action;
            }
        }
        private void Add(Condition condition)
        {
            _conditions.Add(condition);
            condition.Changed += Condition_Changed;
        }
        private void Condition_Changed(object sender, EventArgs e)
        {
            ConditionChanged?.Invoke(this, EventArgs.Empty);
        }
        private readonly List<Condition> _conditions = new List<Condition>();
    }
}
